package cn.niit.letuseatdemo.controller.user;

import cn.hutool.db.sql.Order;
import cn.niit.letuseatdemo.entity.domain.User;
import cn.niit.letuseatdemo.entity.domain.UserOrder;
import cn.niit.letuseatdemo.entity.vo.response.BaseResponse;
import cn.niit.letuseatdemo.service.UserOrderService;
import cn.niit.letuseatdemo.util.H5LoginUtil;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created with IntelliJ IDEA.
 *处理用户订单管理的控制器类
 * @Author: JinNa ToDo
 * @Date: 2022/11/06/16:52
 * @Description:
 */
@Slf4j
@RequestMapping("/api/order")
@RestController
public class UserOrderControlller {

    @Autowired
    private UserOrderService userOrderService;

    /*
    * @Description: 用户下单${TODO}
    * @Author: Jin_Na
    * @CreateTime: 2022/11/13 17:19
 * @param: model
 * @param: userOrder
 * @return: java.lang.String
    **/
    @PostMapping()
    public String insert(@RequestBody UserOrder userOrder){
        userOrderService.insert(userOrder);
        return "order/success";
    }

    /*
    * @Description: 订单列表的初始化及查询${TODO}
    * @Author: Jin_Na
    * @CreateTime: 2022/11/6 17:28
     * @param: null
     * @return: null
    **/
    @GetMapping("/{orderId}")
    public BaseResponse selectByUserOrder(@PathVariable("orderId") Integer orderId){
        List<UserOrder> userOrder = (List<UserOrder>) userOrderService.selectUserOrderByOrderId(orderId);
        return BaseResponse.success(userOrder);
    }


/*
* @Description:用户根据订单状态查询订单列表
* @Author: Jin_Na
* @CreateTime: 2022/12/8 15:27
 * @param: userOrder
 * @return: cn.niit.letuseatdemo.entity.vo.response.BaseResponse
**/
    @GetMapping
    public BaseResponse userOrder(UserOrder userOrder) {
        User user = H5LoginUtil.getLoginUser();//获取当前登录用户
        //用户查询订单需要：订单表需要一个当前登录用户的ID，获取对象需要从当前登录对象里获取对应的id,保证用户看到的是自己的订单
        userOrder.setUserId(user.getUserId());
        List<UserOrder> userOrderList = userOrderService.selectUserOrderList(userOrder);
        Map<String, Object> map = new HashMap<>();
        map.put("userOrderList", userOrderList);
        System.out.println("1111");
        return BaseResponse.success(map);
    }


    /*
    * @Description: 订单删除${TODO}
    * @Author: Jin_Na
    * @CreateTime: 2022/11/8 22:11
     * @param: OrderId
     * @return: java.lang.String
    **/
    @DeleteMapping("/{orderId}")
    @ResponseBody
    public String remove(@PathVariable("orderId") Integer OrderId){
        int rows = userOrderService.deleteUserOrderByOrderId(OrderId);
        if(rows == 1){
            return "true";
        }else{
            return "false";
        }
    }

    /*
    * @Description: 订单状态控制
    * @Author: Jin_Na
    * @CreateTime: 2022/12/5 22:17
     * @param: orderId
     * @return: java.lang.String
    **/
    @GetMapping("/onDown/{orderId}")
    public String onDown (@PathVariable("orderId") Integer orderId) {
        log.info("要修改的ID = " + orderId);
        UserOrder userOrder = null;
        if( null != orderId && orderId > 0) {
            userOrder = userOrderService.selectUserOrderByOrderId(orderId);
        }
        userOrder.setOrderStatus((userOrder.getOrderStatus() + 1)%6);//六种状态
        userOrderService.updateUserOrderStatus(userOrder);
        return "redirect://order";

    }

}
